﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;

namespace Yuanlin.Business
{
    public class ZiCaiInfoListControl : YuanlinUserControl,
        IReplyConverter<ZiCaiInfoReply, GetListOperation>
    {
        protected Literal zicaiInfoLiteral;
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        protected HtmlImage viewImage;
        private EntityList ziCaiInfos;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;
        public int kind = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.Kind);
        public string tradeType = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.TradeType);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack && !AjaxManager.IsCallBack)
            {
                requestBinders.Add(RequestHelper.BuildGetZiCaiInfoListRequest(this, user.EntityId.Value, tradeType, kind, pager.PageIndex, pager.PageSize));
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            AjaxManager.Register(this, "AjaxMethod");
        }

        private void BindData()
        {
            zicaiInfoLiteral.Text = ZiCaiInfoControl.TitleStr(kind, tradeType) +"信息管理";
            if (list == null)
            {
                return;
            }

            list.DataSource = ziCaiInfos;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(ziCaiInfos.TotalCount);
                currentPage.TotalRecords = (ziCaiInfos.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        #region Ajax Method

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void DeleteZiCaiInfo(int ziCaiInfoId)
        {
            ZiCaiInfo ziCaiInfo = BusinessManager.GetZiCaiInfo(ziCaiInfoId);
            if (ziCaiInfo != null)
            {
                BusinessManager.DeleteZiCaiInfo(ziCaiInfoId);
                SiteUrls.Instance().DeleteFile(ziCaiInfo);

                if (ziCaiInfos.Count == 0)
                {
                    if (kind == 1)//机械
                    {
                        if (tradeType == "0")//出售
                        {
                            user.CompanyMachineChuShou = "";
                        }
                        else if (tradeType == "1")//求购
                        {
                            user.CompanyMachineQiuGou = "";
                        }
                    }
                    else if (kind == 2)//设施
                    {
                        if (tradeType == "0")//出售
                        {
                            user.CompanyFacilityChuShou = "";
                        }
                        else if (tradeType == "1")//求购
                        {
                            user.CompanyFacilityQiuGou = "";
                        }
                    }
                    else if (kind == 3)//农资
                    {
                        if (tradeType == "0")//出售
                        {
                            user.CompanyAgriculturalChuShou = "";
                        }
                        else if (tradeType == "1")//求购
                        {
                            user.CompanyAgriculturalQiuGou = "";
                        }
                    }
                    else if (kind == 4)//化肥
                    {
                        if (tradeType == "0")//出售
                        {
                            user.CompanyHuaFeiChuShou = "";
                        }
                        else if (tradeType == "1")//求购
                        {
                            user.CompanyHuaFeiQiuGou = "";
                        }
                    }
                    else if (kind == 5)//饲料
                    {
                        if (tradeType == "0")//出售
                        {
                            user.CompanyMashChuShou = "";
                        }
                        else if (tradeType == "1")//求购
                        {
                            user.CompanyMashQiuGou = "";
                        }
                    }
                    
                   UserManager.UpdateUser(user);
                }
            }
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int DeleteZiCaiInfos(string entityIds)
        {
            if (string.IsNullOrEmpty(entityIds))
            {
                return 0;
            }
            string[] entityIdArray = entityIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int entityId = 0;
            foreach (string stringEntityId in entityIdArray)
            {
                try
                {
                    entityId = Int32.Parse(stringEntityId);
                }
                catch
                {
                    continue;
                }
                if (entityId > 0)
                {
                    DeleteZiCaiInfo(entityId);                
                }
            }
            return 0;
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public void UpdateZiCaiInfo(int ziCaiInfoId)
        {
            ZiCaiInfo ziCaiInfo = BusinessManager.GetZiCaiInfo(ziCaiInfoId);
            if (ziCaiInfo != null)
            {
                ziCaiInfo.UpdateDate.Value = DateTime.Now;
                BusinessManager.UpdateZiCaiInfo(ziCaiInfo);
            }
        }

        [AjaxMethod(IncludeControlValuesWithCallBack = false)]
        public int UpdateZiCaiInfos(string entityIds)
        {
            if (string.IsNullOrEmpty(entityIds))
            {
                return 0;
            }
            string[] entityIdArray = entityIds.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            int entityId = 0;
            foreach (string stringEntityId in entityIdArray)
            {
                try
                {
                    entityId = Int32.Parse(stringEntityId);
                }
                catch
                {
                    continue;
                }
                if (entityId > 0)
                {
                    UpdateZiCaiInfo(entityId);
                }
            }
            return 0;
        }

        #endregion

        #region IReplyConverter<ZiCaiInfoReply,GetListOperation> Members

        public void ConvertReply(ZiCaiInfoReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                ziCaiInfos = reply.EntityList;

                if (!Page.IsPostBack && !AjaxManager.IsCallBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}